<?php
include("Battleship.php");
include("Player.php");

class Game {
        
        	private static $_instance = null;

        	private $idGame;
        	private $player_1;
                private $player_2;

	public function __construct($v = null) {
		$db = new Db();
		$this->dbh = $db->getDbh();
		//id joueur en dur implementation log pas terminée
		$this->newGame(1,2, $v);
		if($v == 2){
			$this->cleanBG();
		}
	}

	public function getInstance() {
		if (is_null(self::$_instance)) {
			self::$_instance = new Game(); 
		}
		return self::$_instance;
	}

	public function newPlayer($id, $i) {
		$player = new Player();
		$player->setId($id);
		$player->setName("Player ".$i);
		return $player;
	}

	public function __get($attr) {
		return $this->$attr;
	}

	public function reconstruct($id1, $id2, $s1, $s2){
		$this->player_1 = $this->newPlayer($id1, 1);
		$this->player_1->ships = $s1;
		$this->player_2 = $this->newPlayer($id2, 2);
		$this->player_2->ships = $s2;
	}

	public function newGame($idPlayer1, $idPlayer2, $v){
		if($v == 1){
			$this->idGame = $this->initGame($idPlayer1, $idPlayer2);
		}
		else{
			//test
			$this->idGame = 1;
		}
		$battleship = new Battleship();
		$i = 1;
		$this->player_1 = $this->newPlayer($idPlayer1, $i);
		$this->player_1->ships = $battleship->generateShips();
		$i++;
		$this->player_2 = $this->newPlayer($idPlayer2, $i);
		$this->player_2->ships = $battleship->generateShips();
	}

	public function generateBattleground() {
	// class parent, implementation in Battleground.php
	}
	public function cleanBG(){
		$sql = "UPDATE `battleground` SET `battleground_state` =0";
		$stmt = $this->dbh->prepare($sql);
		$stmt->execute();
	}

	public function checkBattleground($idPlayer, $idGame, $x, $y) {
		$sql = "SELECT battleground_id, battleground_state 
		FROM battleground
		WHERE player_id = :idPlayer
		AND game_id = :idGame
		AND battleground_position_x = :x
		AND battleground_position_y = :y";
		$stmt = $this->dbh->prepare($sql);
		$stmt->bindValue(":idPlayer", $idPlayer);
		$stmt->bindValue(":idGame", $idGame);
		$stmt->bindValue(":x", $x);
		$stmt->bindValue(":y", $y);
		$stmt->execute();

		$bgState = $stmt->fetch();
		return $bgState;
	}

	public function updateBattleground($idGame, $bgState){
		//Choix du résultat après le tir
		if($bgState["battleground_state"] == 0){
		$state = 3;
		}else if($bgState["battleground_state"] == 1){
		$state = 2;
		}
		//Update en base de donnée du champ de bataille
		$sql = "UPDATE battleground SET battleground_state = :state
		WHERE battleground_id = :idBG";
		$stmt->$this->dbh->prepare($sql);
		$stmt->bindValue(":state", $state);
		$stmt->bindValue(":idBG", $bgState["battleground_id"]);
		$stmt->execute();
	}
        
        public function initGame($idPlayer1, $idPlayer2){
            $sql = "INSERT INTO game (game_player1, game_player2, game_state)
                    VALUES (:idPlayer1, :idPlayer2, 0)";
            $stmt = $this->dbh->prepare($sql);
            $stmt->bindValue(":idPlayer1", $idPlayer1);
            $stmt->bindValue(":idPlayer2", $idPlayer2);
            $stmt->execute();
            
            return $this->dbh->lastInsertId();
        }

	public function updateGame($idGame, $idPlayer1, $idPlayer2){
		//Update en base de donnée de la partie
		$sql = "SELECT game_state, game_player1, game_player2 FROM game
		WHERE game_id = :idGame";
		$stmt = $this->dbh->prepare($sql);
		$stmt->bindValue(":idGame", $idGame);
		$stmt->execute();

		$game = $stmt->fetch();

		if($game['game_state'] == 1 && $game["game_player1"] == $idPlayer1){
                        $state = 2;
		}else if($game["game_state"] == 2 && $game["game_player2"] == $idPlayer2){
                        $state = 1;
		}
		$sql = "UPDATE game SET game_state = :state 
		WHERE game_id = :idGame";
		$stmt = $this->dbh->prepare($sql);
		$stmt->bindValue(":state", $state);
		$stmt->bindValue(":idGame", $idGame);
		$stmt->execute();
	}

	public function finishGame($idGame){
		$sql = "SELECT player_id, COUNT(battleground_state) as total
		FROM battleground
		WHERE game_id = :idGame
		AND battleground_state = 2
		GROUP BY player_id";
		$stmt = $this->dbh->prepare($sql);
		$stmt->bindValue(":idGame", $idGame);
		$stmt->execute();

		$gameState = $stmt->fetchAll();

		if($gameState["total"] == 17){
			$sql = "UPDATE game SET game_winner = :idPlayer
			WHERE game_id = :idGame";
			$stmt = $this->dbh->prepare($sql);
			$stmt->bindValue(":idPlayer", $gameState["player_id"]);
			$stmt->bindValue(":idGame", $idGame);
			$stmt->execute();
		}
	}

	public function displayBattleshipP1() {
		$ships = $this->player_1->ships;
		$i = 1;
		echo '<ul class="ship_placement no" id="P1">';
		foreach ($ships as $ship) {
			echo '<li class="ships" data-id="'.$i.'" data-size="'.$ship[1].'" data-name="'.$ship[0].'" >';
			echo '<img src="img/'.$ship[2].'" alt="" />';
			echo '</li>';
			$i++;
		}
		echo '</ul>';
	}
	
	public function displayBattleshipP2() {
		$ships = $this->player_2->ships;
		$i = 1;
		echo '<ul class="ship_placement no" id="P2">';
		foreach ($ships as $ship) {
			echo '<li class="ships" data-id="'.$i.'" data-size="'.$ship[1].'" data-name="'.$ship[0].'">';
			echo '<img src="img/'.$ship[2].'" alt="" />';
			echo '</li>';
			$i++;
		}
		echo '</ul>';
	}
	public function jsonSerializeP1() {
		$ships = $this->player_1->ships;
		return json_encode($ships);
	}
	public function jsonSerializeP2() {
		$ships = $this->player_2->ships;
		return json_encode($ships);
	}

}
